package de.java.shortys.myhome.fuelmonitor.view;

import static de.java.shortys.myhome.fuelmonitor.controller.FuelMonitorController.NAME;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.AMOUNT;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.AMOUNT_SYMBOL;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.CHARGES;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.DATE;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.DISTANCE;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.DISTANCE_SYMBOL;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_AMOUNT;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_CHARGES;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_DATE_FILLED;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_DISTANCE;
import static de.java.shortys.myhome.language.controller.LanguageManager.getInstance;

import java.awt.Color;
import java.awt.Component;
import java.awt.LayoutManager;
import java.util.Map;
import java.util.Observable;
import java.util.TreeMap;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import de.java.shortys.myhome.api.validation.exception.AttributeFieldNotFoundException;
import de.java.shortys.myhome.api.validation.exception.AttributeInformationFieldNotFoundException;
import de.java.shortys.myhome.api.view.IView;

public class FuelFillingDetailsView implements IView {

	private static final String NAME_DISTANCE = getInstance(NAME).get(DISTANCE);
	private static final String SYMBOL_DISTANCE = getInstance(NAME).get(DISTANCE_SYMBOL);
	private static final String NAME_AMOUNT = getInstance(NAME).get(AMOUNT);
	private static final String SYMBOL_AMOUNT = getInstance(NAME).get(AMOUNT_SYMBOL);
	private static final String NAME_CHARGES = getInstance(NAME).get(CHARGES);
	private static final String NAME_DATE = getInstance(NAME).get(DATE);

	private Map<String, JComponent> attributeFieldMapping;
	private Map<String, JLabel> attributeInformationFieldMapping;
	private Map<String, String> attributeNameMapping;

	{
		attributeFieldMapping = new TreeMap<String, JComponent>();
		attributeInformationFieldMapping = new TreeMap<String, JLabel>();
		attributeNameMapping = new TreeMap<String, String>();
	}

	private JPanel contentPane;
	private LayoutManager cotentPaneLayoutManager;

	private JLabel lDateFilledInformation;
	private JLabel lDateFilled;
	private JTextField tfDateFilled;

	private JLabel lDistanceInformation;
	private JLabel lDistance;
	private JTextField tfDistance;

	private JLabel lAmountInformation;
	private JLabel lAmount;
	private JTextField tfAmount;

	private JLabel lChargesInformation;
	private JLabel lCharges;
	private JTextField tfCharges;

	@Override
	public Component build() {
		if (contentPane == null) {
			initialize();
		}
		return contentPane;
	}

	public void setValid(String attributeId, boolean valid) throws AttributeFieldNotFoundException,
			AttributeInformationFieldNotFoundException {
		JLabel attributeInformationField = attributeInformationFieldMapping.get(attributeId);
		JComponent attributeField = attributeFieldMapping.get(attributeId);

		if (attributeField == null) {
			throw new AttributeFieldNotFoundException(attributeId);
		}
		if (attributeInformationField == null) {
			throw new AttributeInformationFieldNotFoundException(attributeId);
		}

		if (valid) {
			attributeInformationField.setText("");
			attributeField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		} else {
			attributeInformationField.setText(attributeNameMapping.get(attributeId)
					+ " konnte nicht korrekt validiert werden");
			attributeField.setBorder(BorderFactory.createLineBorder(Color.RED));
		}
	}

	private void initialize() {
		createContentPane();
		createDateFilled();
		createDistance();
		createAmount();
		createCharges();
		initializeFieldMapping();
		initializeInformationFieldMapping();
		initializeAttributeNameMapping();
	}

	private void initializeAttributeNameMapping() {
		attributeNameMapping.put(ATTR_DISTANCE, NAME_DISTANCE);
		attributeNameMapping.put(ATTR_DATE_FILLED, NAME_DATE);
		attributeNameMapping.put(ATTR_AMOUNT, NAME_AMOUNT);
		attributeNameMapping.put(ATTR_CHARGES, NAME_CHARGES);
	}

	private void initializeFieldMapping() {
		attributeFieldMapping.put(ATTR_DISTANCE, tfDistance);
		attributeFieldMapping.put(ATTR_DATE_FILLED, tfDateFilled);
		attributeFieldMapping.put(ATTR_AMOUNT, tfAmount);
		attributeFieldMapping.put(ATTR_CHARGES, tfCharges);
	}

	private void initializeInformationFieldMapping() {
		attributeInformationFieldMapping.put(ATTR_AMOUNT, lAmountInformation);
		attributeInformationFieldMapping.put(ATTR_CHARGES, lChargesInformation);
		attributeInformationFieldMapping.put(ATTR_DATE_FILLED, lDateFilledInformation);
		attributeInformationFieldMapping.put(ATTR_DISTANCE, lDistanceInformation);
	}

	private void createAmount() {
		lAmountInformation = new JLabel();
		contentPane.add(lAmountInformation, "span 2, wrap");

		lAmount = new JLabel(NAME_AMOUNT + " (" + SYMBOL_AMOUNT + ")");
		contentPane.add(lAmount);

		tfAmount = new JTextField();
		contentPane.add(tfAmount, "wrap");
	}

	private void createCharges() {
		lChargesInformation = new JLabel();
		contentPane.add(lChargesInformation, "span 2, wrap");

		lCharges = new JLabel(NAME_CHARGES);
		contentPane.add(lCharges);

		tfCharges = new JTextField();
		contentPane.add(tfCharges, "wrap");
	}

	private void createContentPane() {
		cotentPaneLayoutManager = new MigLayout("", "[]20[grow, fill]", "");
		contentPane = new JPanel(cotentPaneLayoutManager);
		contentPane.setOpaque(false);
	}

	private void createDateFilled() {
		lDateFilledInformation = new JLabel();
		contentPane.add(lDateFilledInformation, "span 2, wrap");

		lDateFilled = new JLabel(NAME_DATE);
		contentPane.add(lDateFilled);

		tfDateFilled = new JTextField();
		contentPane.add(tfDateFilled, "wrap");
	}

	private void createDistance() {
		lDistanceInformation = new JLabel();
		contentPane.add(lDistanceInformation, "span 2, wrap");

		lDistance = new JLabel(NAME_DISTANCE + " (" + SYMBOL_DISTANCE + ")");
		contentPane.add(lDistance);

		tfDistance = new JTextField();
		contentPane.add(tfDistance, "wrap");
	}

	public JTextField getTfAmount() {
		return tfAmount;
	}

	public JTextField getTfCharges() {
		return tfCharges;
	}

	public JTextField getTfDateFilled() {
		return tfDateFilled;
	}

	public JTextField getTfDistance() {
		return tfDistance;
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clear() {
		tfDateFilled.setText("");
		tfDistance.setText("");
		tfAmount.setText("");
		tfCharges.setText("");
	}
}
